package com.hngy.xpq.shoppingcartmodule.ui.notifications;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.lifecycle.ViewModel;

import com.hngy.xpq.shoppingcartmodule.R;
import com.hngy.xpq.shoppingcartmodule.bean.Shop;
import com.hngy.xpq.shoppingcartmodule.database.SqlShop;

public class NotificationsViewModel extends ViewModel {

    private Shop shop;
    private EditText name, price, bus, dis, time, des;
    private SqlShop sqlShop;

    // 存储数据
    public void setShop(View root) {
        sqlShop = new SqlShop(root.getContext());  // 初始化数据库
        shop = new Shop();                         // 定义实体
        name = root.findViewById(R.id.name);
        price = root.findViewById(R.id.price);
        bus = root.findViewById(R.id.bus);
        dis = root.findViewById(R.id.dis);
        time = root.findViewById(R.id.time);
        des = root.findViewById(R.id.des);
        shop.setImage("");
        shop.setName(name.getText().toString());
        shop.setBus(bus.getText().toString());
        shop.setTime(time.getText().toString());
        shop.setDes(des.getText().toString());
        try {   // 存在两个非String类型数据需要做转型异常判断
            shop.setPrice(Integer.valueOf(price.getText().toString()));
            shop.setDis(Float.valueOf(dis.getText().toString()));
        } catch (NumberFormatException e) {
            Toast.makeText(root.getContext(), "请输入正确的数据!", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
        if (sqlShop.insert(shop)) {  // 数据库字段有长度限定。如果需要具体到每个字段，自己写判断语句。
            Log.i("data", shop.toString());
            Toast.makeText(root.getContext(), "数据库插入成功!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(root.getContext(), "数据库插入失败!", Toast.LENGTH_SHORT).show();
        }
    }

    public void qkEditText(View root) {
        name = root.findViewById(R.id.name);
        price = root.findViewById(R.id.price);
        bus = root.findViewById(R.id.bus);
        dis = root.findViewById(R.id.dis);
        time = root.findViewById(R.id.time);
        des = root.findViewById(R.id.des);
        name.setText("");
        price.setText("");
        bus.setText("");
        dis.setText("");
        time.setText("");
        des.setText("");
    }
}